package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.common.constants.WmNewsMessageConstants;
import com.heima.model.article.pojos.ApArticleConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 文章上下架监听类
 */
@Component
public class DownOrUpListener {

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void getMessage(String message){
        //1.转类型格式
        Map map = JSON.parseObject(message, Map.class);
        //2.获取属性值
        Object articleId = map.get("articleId");
        /**
         * enable=0 表示下架
         * enable=1 表示上架
         * 目前apArticleConfig表中的字段is_down
         *          true: 下架   1
         *          false: 没有下架(上架)  0
         */
        Integer enable = (Integer) map.get("enable");
        //3.同步aparticleConfig表中的is_down字段值
        ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(
                Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, articleId)
        );
        if(apArticleConfig!=null){
            //修改状态
            apArticleConfig.setIsDown(enable==1?false:true);
            apArticleConfigMapper.updateById(apArticleConfig);
        }
    }
}
